# Copyright (C) Kumo inc. and its affiliates.
# Author: Jeff.li lijippy@163.com
# All rights reserved.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https:#www.gnu.org/licenses/>.
#

add_subdirectory(utils)
add_library(pollux_dwio_faulty_file_sink FaultyFileSink.cpp)
target_link_libraries(
  pollux_dwio_faulty_file_sink pollux_file_test_utils pollux_dwio_common)

# There is an issue with the VTT symbol for the InlineExecutor from melon when
# building on Linux with Clang15. It is not created and results in a SEGV when
# attempting to construct the InlineExecutor. nm output shows no address for the
# "v VTT for melon::InlineExecutor" symbol while for a debug build it shows
# "000000000185d3e0 V VTT for melon::InlineExecutor" symbol. It seems to have
# been optimized away by Clang15 and may be some kind of bug. Changing the
# optimization level to 0 results in proper creation/linkage and successful
# execution of the test. Review if this is still necessary when upgrading Clang.
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  set_property(
    SOURCE ParallelForTest.cpp
    APPEND
    PROPERTY COMPILE_OPTIONS -O0)
endif()

add_executable(
  pollux_dwio_common_test
  BitConcatenationTest.cpp
  BitPackDecoderTest.cpp
  ChainedBufferTests.cpp
  ColumnSelectorTests.cpp
  DataBufferTests.cpp
  DecoderUtilTest.cpp
  ExecutorBarrierTest.cpp
  OnDemandUnitLoaderTests.cpp
  LocalFileSinkTest.cpp
  MemorySinkTest.cpp
  LoggedExceptionTest.cpp
  MeasureTimeTests.cpp
  ParallelForTest.cpp
  RangeTests.cpp
  ReadFileInputStreamTests.cpp
  ReaderTest.cpp
  RetryTests.cpp
  TestBufferedInput.cpp
  ThrottlerTest.cpp
  TypeTests.cpp
  UnitLoaderToolsTests.cpp
  WriterTest.cpp
  OptionsTests.cpp)
add_test(pollux_dwio_common_test pollux_dwio_common_test)
target_link_libraries(
  pollux_dwio_common_test
  pollux_dwio_common_test_utils
  pollux_temp_path
  pollux_vector_test_lib
  Boost::regex
  pollux_link_libs
  Melon::melon
  ${TEST_LINK_LIBS}
  GTest::gtest
  GTest::gtest_main
  GTest::gmock
  fmt::fmt
  protobuf::libprotobuf)

if(POLLUX_ENABLE_BENCHMARKS)
  add_executable(pollux_dwio_common_data_buffer_benchmark
                 DataBufferBenchmark.cpp)

  target_link_libraries(
    pollux_dwio_common_data_buffer_benchmark
    pollux_dwio_common
    pollux_memory
    pollux_dwio_common_exception
    Melon::melon
    Melon::melonbenchmark)

  add_executable(pollux_dwio_common_int_decoder_benchmark
                 IntDecoderBenchmark.cpp)
  target_link_libraries(
    pollux_dwio_common_int_decoder_benchmark
    pollux_dwio_common_exception
    pollux_exception
    pollux_dwio_dwrf_common
    Melon::melon
    Melon::melonbenchmark)

  if(POLLUX_ENABLE_ARROW)
    add_subdirectory(Lemire/FastPFor)
    add_executable(pollux_dwio_common_bitpack_decoder_benchmark
                   BitPackDecoderBenchmark.cpp)

    target_compile_options(pollux_dwio_common_bitpack_decoder_benchmark
                           PRIVATE -Wno-deprecated-declarations)

    target_link_libraries(
      pollux_dwio_common_bitpack_decoder_benchmark
      pollux_dwio_common
            Arrow::arrow_static
      pollux_fastpforlib
      duckdb_static
      Melon::melon
      Melon::melonbenchmark)
  endif()
endif()
